﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: 


版本: 1


创建日期:2010-5-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-5-3

ToDo:

***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using MF.Data.Manager;
using MF.Data.SupplyInterface;

namespace MF.Data.WebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class HostService : System.Web.Services.WebService
    {

        [WebMethod]
        public MFServiceInformatiom GetServiceInformatiom()
        {
            MFServiceInformatiom msi = new MFServiceInformatiom();
            msi.Version="0.1";
            msi.ServerType = "web";
            msi.ServerTime =DateTime.Now;
            return msi;
        }

        [WebMethod]
        public DataSet GetDataSet(string sql)
        {
            MFDataManager dm=new MFDataManager();
            
            DataSet ds = dm.GetDataSet(sql);
            return ds;
        }

        [WebMethod]
        public int UpdataDataSet(DataSet dataSet)
        {
            try
            {
                MFDataManager dm = new MFDataManager();

                dm.UpdateTable(dataSet.Tables[0], dataSet.Tables[0].TableName);
                return 0;
            }
            catch 
            {
                return -1;
            }
        }

        [WebMethod]
        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleInfo info = new MFDBResuleInfo();
            MFDataManager dm = new MFDataManager(); 
            try
            {
                
                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;
                if (dataParameters!=null)
                foreach (MFDataParameter param in dataParameters)
                {
                   
                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);
                    
                    cmd.Parameters.Add(dp);
                }
               
                cmd.Prepare();
               
                info.Count = cmd.ExecuteNonQuery();

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction!=ParameterDirection.Input)
                    dataParameters[param.ParameterName].Value = param.Value;
                }
                info.DataParameters = dataParameters;
                info.IsSuccess=true;
                info.Code=0;
                dm.Close();
                return info;
            }
            catch(Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }

        [WebMethod]
        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleDataSetInfo info = new MFDBResuleDataSetInfo();
            MFDataManager dm = new MFDataManager();
            try
            {
                
                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;

                if (dataParameters != null)
                foreach (MFDataParameter param in dataParameters)
                {

                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);

                    cmd.Parameters.Add(dp);
                }

                IDataAdapter da = dm.GetDataAdapter(cmd);
                
                cmd.Prepare();
                //info.Count = cmd.ExecuteNonQuery();
                DataSet ds = new DataSet();
                dm.CheckedOpen();
                da.Fill(ds);
                if (ds.Tables.Count > 0)
                    info.Count = ds.Tables[0].Rows.Count;
                else
                    info.Count = 0;

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                        dataParameters[param.ParameterName].Value = param.Value;
                }

                info.DataParameters = dataParameters;
                info.ResultDataSet = ds;
                info.IsSuccess = true;
                info.Code = 0;
                dm.Close();
                return info;
            }
            catch (Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }
    }


    [Serializable]
    public class MFServiceInformatiom
    {
        public string Version { get; set; }
        public string ServerType { get; set; }
        public DateTime ServerTime { get; set; }

    }
}
